package com.wzf.greattruth.test.zookeeper.lock;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;

import com.alibaba.fastjson.JSON;

public class ZookeeperTester {
    private static String HOST = "localhost:2181";
    private static int SESSION_TIMEOUT = 1000 * 60 * 60;

    private byte[] datas = null;
    private CreateMode mode = CreateMode.PERSISTENT;

    protected static final List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;

    private static ZooKeeper zookeeper;

    public static void main(String[] args) throws Exception {
        zookeeper = new ZooKeeper(HOST, SESSION_TIMEOUT, null);

        ZookeeperTester tester = new ZookeeperTester();
        tester.testCreateEmpNode();
        zookeeper.close();

    }

    private void testCreateEmpNode() {
        CreateMode em = CreateMode.EPHEMERAL;
        CreateMode pm = CreateMode.PERSISTENT;
        String dir1 = File.separator + "wzf" + File.separator + "test1";

        // createNode(dir1, pm);
        getChildren(dir1);

        String node = dir1 + File.separator + "node_1";
        createNode(node, em);
        getChildren(dir1);
        
        createNode(node, em);
        getChildren(dir1);

        try {
            zookeeper.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.err.println();
    }

    private void createNode(String dir, CreateMode mode) {
        String node = null;
        try {
            node = zookeeper.create(dir, datas, acl, mode);
        } catch (KeeperException.NodeExistsException e) {
            node = dir;
        } catch (KeeperException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.err.println("createNode! node=" + node);
    }

    private void deleteNode(String path) {
        boolean success = false;
        try {
            zookeeper.delete(path, -1);
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.err.println("deleteNode! path=" + path + "  result=" + success);
    }

    private void getChildren(String dir) {
        List<String> children = null;
        try {
            children = zookeeper.getChildren(dir, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.err.println("createNode! children=" + JSON.toJSONString(children));
    }

    private void testCreatePerisistentNode() {
        String dir = File.separator + "wzf" + File.separator + "test1";
        try {
            zookeeper.create(dir, datas, acl, mode);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            zookeeper.delete(dir, -1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.err.println();
    }

    private ZooKeeper createZookeeper() {
        ZooKeeper zookeeper = null;
        try {
            zookeeper = new ZooKeeper(HOST, SESSION_TIMEOUT, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return zookeeper;
    }

}
